package com.baor.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baor.dao.EquipmentHolderOrdersDao;
import com.baor.domain.Equipment_holder_orders;
import com.baor.service.OrderService;
import com.baor.util.R;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private EquipmentHolderOrdersDao equipmentHolderOrdersDao;
    //TODO 设备持有人订单
    @Override
    public R getHolerList(Map<String, Object> params) {
        Page page = new Page<>();
        page.setCurrent((Integer) params.get("pageIndex"));
        page.setSize((Integer) params.get("pageSize"));
        MPJLambdaWrapper<Equipment_holder_orders> wrapper = new MPJLambdaWrapper<>(Equipment_holder_orders.class)
                .select(
                        Equipment_holder_orders::getOrderCode,
                        Equipment_holder_orders::getUsername,
                        Equipment_holder_orders::getPhone,
                        Equipment_holder_orders::getMoney,
                        Equipment_holder_orders::getPayMethod,
                        Equipment_holder_orders::getPayTime,
                        Equipment_holder_orders::getRemarks)
                .eq(Equipment_holder_orders::getRole,0);
        String phone = (String) params.get("phone");
        String name = (String) params.get("username");
        if (StringUtils.hasText(phone)) {
            wrapper.like(Equipment_holder_orders::getPhone, phone);
        }
        if (StringUtils.hasText(name)) {
            wrapper.like( Equipment_holder_orders::getUsername, name);
        }
        Page<Equipment_holder_orders> List = equipmentHolderOrdersDao.selectJoinPage(page, Equipment_holder_orders.class, wrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("totalSize", List.getTotal());
        map.put("list", List.getRecords());
        return R.ok(map);
    }

    //TODO 代理商订单
    @Override
    public R getAgentList(Map<String, Object> params) {
        Page page = new Page<>();
        page.setCurrent((Integer) params.get("pageIndex"));
        page.setSize((Integer) params.get("pageSize"));
        MPJLambdaWrapper<Equipment_holder_orders> wrapper = new MPJLambdaWrapper<>(Equipment_holder_orders.class)
                .select(
                        Equipment_holder_orders::getOrderCode,
                        Equipment_holder_orders::getUsername,
                        Equipment_holder_orders::getPhone,
                        Equipment_holder_orders::getMoney,
                        Equipment_holder_orders::getPayMethod,
                        Equipment_holder_orders::getPayTime,
                        Equipment_holder_orders::getRemarks)
                .eq(Equipment_holder_orders::getRole,1);
        String phone = (String) params.get("phone");
        String name = (String) params.get("username");
        if (StringUtils.hasText(phone)) {
            wrapper.like(Equipment_holder_orders::getPhone, phone);
        }
        if (StringUtils.hasText(name)) {
            wrapper.like( Equipment_holder_orders::getUsername, name);
        }
        Page<Equipment_holder_orders> List = equipmentHolderOrdersDao.selectJoinPage(page, Equipment_holder_orders.class, wrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("totalSize", List.getTotal());
        map.put("list", List.getRecords());
        return R.ok(map);
    }
}
